﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WcfService.Util;
using WcfService.DTO;
using System.Data;
using System.Data.OleDb;

namespace WcfService.DAO
{
    public class TinTucDAO : DataProvider
    {
        public TinTucDAO()
        {
            connect();
        }
        public TinTucDTO[] getAll()
        {
            string query = "SELECT * FROM TinMuaBan";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            TinTucDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new TinTucDTO[n];
            for (i = 0; i < n; i++)
            {
                object tt = GetDataFromDataRow(dt, i);
                arr[i] = (TinTucDTO)tt;
            }
            return arr;
        }
        public TinTucDTO[] getNews(string ma)
        {
            int id = Int32.Parse(ma);
            string query = "SELECT * FROM TinMuaBan Where MaTinMuaBan=" + id;
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            TinTucDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new TinTucDTO[n];
            for (i = 0; i < n; i++)
            {
                object tt = GetDataFromDataRow(dt, i);
                arr[i] = (TinTucDTO)tt;
            }
            return arr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            TinTucDTO tt = new TinTucDTO();
            tt.MaTin = int.Parse(dt.Rows[i]["MaTinMuaBan"].ToString());
            tt.TieuDe = dt.Rows[i]["TieuDe"].ToString();
            tt.NguoiDung = dt.Rows[i]["NguoiDung"].ToString();
            tt.NoiDung = dt.Rows[i]["NoiDung"].ToString();
            tt.TinhTrang = int.Parse(dt.Rows[i]["TinhTrang"].ToString());

            return (object)tt;
        }

        public void insert(string tieuDe, string nguoiDung, string noiDung, int tinhTrang)
        {
            string insertCommand = "INSERT INTO TinMuaBan VALUES('" +
                                   tieuDe + "','" +
                                   nguoiDung + "','" +
                                   noiDung + "'," +
                                   tinhTrang + ")";

            executeNonQuery(insertCommand);
            disconnect();
        }

        public void delete(int maTin)
        {
            string deleteCommand = "UPDATE TinMuaBan SET TinhTrang = 0 WHERE MaBinhLuan = " + maTin;
            executeNonQuery(deleteCommand);
            disconnect();
        }

        public void update(int ma, string tieuDe, string nguoiDung, string noiDung, int tinhTrang)
        {
            string updateCommand = "UPDATE TinMuaBan SET TieuDe = '" + tieuDe + "',"
                + "NguoiDung = '" + nguoiDung + "',"
                 + "NoiDung = '" + noiDung + "',"
                + "TinhTrang = " + tinhTrang
                + "WHERE MaTinMuaBan = " + ma;
            executeNonQuery(updateCommand);
            disconnect();
        }
    }
}